using System;
using System.Xml;
using System.Collections;

namespace ScenarioFramework.Modules.PCM.DataModel.ConfigurationManager.Entity
{
	//*********************************************************************
	//
	// Entity Class
	//
	// The Entity class represents the Entity of the product thath we are
	// configuring sored in a file thath rapprsents the source of the 
	// Product configuration
	//
	//*********************************************************************

	public class XMLEntity : bOSdotNET.XML.Data.XMLObject
	{
		public const string MAIN_NODE	= "Entity";

		private const string NAME_ATTRIBUTE					= "Name";
		private const string ALIAS_ATTRIBUTE				= "Alias";
		private const string ENTITYAGGREGATION_ATTRIBUTE	= "EntityAggregation";
		private const string ID_ATTRIBUTE					= "Id";

		private const string DESCRIPTION_NODE	= "Description";

		public const string ENTITYAGGREGATION_MECCANICA	=  "Meccanica";
		public const string ENTITYAGGREGATION_ELETTRICA	=  "Elettrica";
		public const string ENTITYAGGREGATION_IDRAULICA	=  "Idraulica";
		public const string ENTITYAGGREGATION_ALTRO		=  "Altro";
		
		/** FIELDS **/

		private string sName;
		private string sEntityAggregation;
		private string sDescription;
		private string sAlias;
		private string sId;

		private XMLEntityAttributes vAttributes;

		/** CONSTRUCTORS **/

		/** CONSTRUCTOR Width simple Property **/
		public XMLEntity()
		{
			this.Init();
		}
		
		/** CONSTRUCTOR Width xml parsering**/
		public XMLEntity(System.Xml.XmlNode node)
		{
			this.Parse(node);
		}

		public XMLEntity(string sEntityName,string sEntityId)
		{
			this.Init();

			this.Name = sEntityName;
			this.Id = sEntityId;
		}

		public XMLEntity(string sEntityName)
		{
			this.Init();

			sName = sEntityName;
		}

		override public void Init()
		{
			sName= string.Empty;
			sEntityAggregation= string.Empty;
			sDescription= string.Empty;
			sAlias = string.Empty;
			sId = string.Empty;

			vAttributes = new XMLEntityAttributes();
		}

		/** PROPERTIES **/
		public XMLEntityAttributes XMLEntityAttributes 
		{
			get { return vAttributes; }
			set { vAttributes = value; }
		}

		public string Name 
		{
			get { return sName; }
			set { sName = value; }
		}

		public string Alias 
		{
			get { return sAlias; }
			set { sAlias = value; }
		}

		public string EntityAggregation 
		{
			get { return sEntityAggregation; }
			set { sEntityAggregation = value; }
		}
		
		public string Description 
		{
			get { return sDescription; }
			set { sDescription = value; }
		}

		public string Id
		{
			get { 
				if ( sId.CompareTo ( string.Empty ) == 0 )
					return this.Name;
				return this.sId;
			}
			set { sId = value;}
		}


		/** INSTANCE METHODS **/

		/** Parsering method for filling the entity **/
		override public void Parse(System.Xml.XmlNode node2Parse)
		{
			this.Init();

			System.Xml.XmlNode node;

			/** find the attributes situated on the root tag **/
			for ( int i=0; i<node2Parse.Attributes.Count;i++)
			{
				node = node2Parse.Attributes.Item(i);

				switch ( node.Name )
				{
					case XMLEntity.NAME_ATTRIBUTE:
						sName = node.InnerText;
						break;
					case XMLEntity.ENTITYAGGREGATION_ATTRIBUTE:
						sEntityAggregation = node.InnerText;
						break;

					case XMLEntity.ALIAS_ATTRIBUTE:
						sAlias = node.InnerText;
						break;

					case XMLEntity.ID_ATTRIBUTE:
						this.sId = node.InnerText;
						break;
				}
			}

			if ( sAlias == string.Empty )
			{
				sAlias = sName;
			}

			/** find the sub tags witch contains Entity values **/
			for ( int i=0; i<node2Parse.ChildNodes.Count; i++)
			{
				node = node2Parse.ChildNodes[i];

				switch (node.Name) 
				{
					case XMLEntity.DESCRIPTION_NODE:
						sDescription = node.InnerText;
						break;

					/** define the subcollection of the EntityAtributes By parsering the subtag Attributes **/
					case XMLEntityAttributes.MAIN_NODE:
						vAttributes.Parse(node);
						break;
				} 
			}
		}

		public bOSdotNET.XML.XMLNode GetShortXML()
		{
			bOSdotNET.XML.XMLNode xmlNode = new bOSdotNET.XML.XMLNode(XMLEntity.MAIN_NODE);
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLEntity.NAME_ATTRIBUTE, sName) );
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLEntity.ID_ATTRIBUTE, this.Id) ); 
			xmlNode.AddNode ( vAttributes.GetShortXML() );
			
			return xmlNode;
		}

		override public bOSdotNET.XML.XMLNode GetXMLNode()
		{
			bOSdotNET.XML.XMLNode xmlNode = new bOSdotNET.XML.XMLNode(XMLEntity.MAIN_NODE);
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLEntity.NAME_ATTRIBUTE, sName) );
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLEntity.ALIAS_ATTRIBUTE, sAlias) );
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLEntity.ENTITYAGGREGATION_ATTRIBUTE, sEntityAggregation) );
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLEntity.ID_ATTRIBUTE, this.Id) ); 
			xmlNode.AddNode ( new bOSdotNET.XML.XMLNode (XMLEntity.DESCRIPTION_NODE, sDescription) );
			xmlNode.AddNode ( vAttributes.GetXMLNode() );
			
			return xmlNode;
		}
	}
}